package team.first.mongodb;

import cn.edu.nxu.it.movie.entity.User;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.BeanListHandler;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import dev.morphia.Datastore;
import dev.morphia.Morphia;
import team.first.mongodb.doc.CommentDoc;
import team.first.mongodb.doc.FilmographyDoc;
import team.first.mongodb.doc.GenreDoc;
import team.first.mongodb.doc.StaffDoc;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: team.first.mongodb.entity
 * @Date: 2021/11/18 20:36
 * @Description：
 */
public class MorphiaCommentImport {
    public static final String MONGO_DB_NAME = "movie_db";
    public static final String MONGO_DB_USERNAME = "root";
    public static final String MONGO_DB_PASSWORD = "123456";

    public static void main(String[] args) {
        // Mongo数据库地址
        ServerAddress serverAddress = new ServerAddress("162.14.114.84", 27017);

        // Mongo数据库的认证信息
        MongoCredential mongoCredential = MongoCredential.createCredential(MONGO_DB_USERNAME, MONGO_DB_NAME,
                MONGO_DB_PASSWORD.toCharArray());
        MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
                .applyToClusterSettings(builder ->
                        builder.hosts(Arrays.asList(serverAddress)))
                .credential(mongoCredential).build();

        // MongoClient必须是com.mongodb.client.MongoClient
        MongoClient mongoClient = MongoClients.create(mongoClientSettings);
        Datastore datastore = Morphia.createDatastore(mongoClient, MONGO_DB_NAME);
        datastore.getMapper().mapPackage("team.first.mongodb.doc");

        // 将数据库中结果插入
        // String sqlGetAllGenres = "SELECT * FROM genre";
        String sqlGetAllComments = "select * from comment";
        String sqlGetUserInfo = "select nickname,avatar from public.user where id = ?";

        try {
            List<Entity> entityList = Db.use().query(sqlGetAllComments);
            List<CommentDoc> commentDocs = new ArrayList<>();

            entityList.forEach(entity -> {
                List<Entity> user = null;
                try {
                    user = Db.use().query(sqlGetUserInfo, entity.getInt("user_id"));
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                CommentDoc commentDoc = new CommentDoc();
                commentDoc.setId(entity.getInt("id"));
                commentDoc.setMovieId(entity.getInt("movie_id"));
                commentDoc.setUserId(entity.getInt("user_id"));
                commentDoc.setContent(entity.getStr("content"));
                commentDoc.setCreatedAt(entity.getStr("created_at"));
                commentDoc.setRating(entity.getInt("rating"));
                commentDoc.setNickname(user.get(0).getStr("nickname"));
                commentDoc.setAvatar(user.get(0).getStr("avatar"));
                commentDocs.add(commentDoc);
            });
            datastore.save(commentDocs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
